// pages/goodsManagement/cloudGoodsDetails/cloudGoodsDetails.js
const Controllers = require('../../controllers/Controller.js').controller;
// const formatTime2 = require('../../../utils/util.js').formatTime2
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current:0,
    isDown:false,
    heat:true,
    goodsId:'',
    goodsDetails:{},
    commentTotalRow:0,
  },

  changeImg(e){
    this.setData({
      current:e.detail.current+1
    })
  },

  //获取商品详情
  getGoodsDetails(){
    Controllers.goodsDetails({goodsId:this.data.goodsId}).then(res=>{
      if(res.code==1){
        let heat
        if(res.data.heat==1){
          heat = true
        }else{
          heat = false
        }
        let isDown
        if(res.data.state==1){
          isDown = true
        }else{
          isDown = false
        }
        // res.data.oc.createTime = formatTime2(res.data.createTime)
        this.setData({
          goodsDetails:res.data,
          heat,
          isDown
        })
      }
    })
  },
  //上架
  upGoods(){
    Controllers.setCloudUp({goodsId:this.data.goodsId,shopId:wx.getStorageSync('shopId')}).then(res=>{
      if(res.code==1){
        wx.showToast({
          title: "上架成功",
          icon: 'none',
          duration: 800
        });
        this.getGoodsDetails()
      }
    })
  },
  //下架
  downGoods(){
    Controllers.setCloudLow({goodsId:this.data.goodsId,shopId:wx.getStorageSync('shopId')}).then(res=>{
      if(res.code==1){
        wx.showToast({
          title: "下架成功",
          icon: 'none',
          duration: 800
        });
        this.getGoodsDetails()
      }
    })
  },
  //获取评论列表
  getGoodsCommentPage(){
    Controllers.goodsCommentPage({pageNumber:1,pageSize:20,goodsId:this.data.goodsId}).then(res=>{
      this.setData({
        commentTotalRow:res.data.totalRow
      })
    })
  },
  //跳转页面
  gopage(e){
    let url = e.currentTarget.dataset['url'];
    wx.navigateTo({
      url: url
    })
  },
  goBack() {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodsId:options.id
    })
    this.getGoodsDetails()
    this.getGoodsCommentPage()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
   // onShareAppMessage: function () {

  // }
})